package dbinit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import core.category.model.Category;
import core.product.model.Product;
import core.util.HibernateCoreUtil;

public class InitCategory 
{
	public static Category getCategoryByUuid(Long uuid)
	{
		Category c = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				c = (Category) session.get(Category.class, uuid);
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return c ;
	}
	public static void setProduct(Category c , Product p)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			if(c.getProducts().contains(p))
			{
				System.out.println("allready there ");
			}
			else
			{
				c.getProducts().add(p);
				session.merge(c);
				transaction.commit();
			}
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally 
		{
			session.close();
		}
	}
	public static List<Category> getCategoriesByName(String name)
	{
		List<Category> cateList = new ArrayList<Category>(); 
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			cateList = session.createQuery("from Category c where c.name='"+name+"'").list();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return cateList ;
	}
	public static Category getCategoryByNameAndPath(String name, String path)
	{
		Category cat = null ;
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			List <Category> cs = session.createQuery("from Category c where c.name='"+name+"' and c.path='"+path+"'").list();
			if(cs.size() != 0)
			{
				Iterator<Category> csItr = cs.iterator();
				while(csItr.hasNext())
				{
					cat = csItr.next();
					break ;
				}
			}
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		
		return cat ;
	}
	//System.out.println("joooooo  "+newCateName + "    " + newPath + "  " + parent + "  " + depth);
	public static Long doinit(String name ,int depth ,String path ,Long parentId, String type)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Long uuid = null;
		Category c =  getCategoryByNameAndPath(name , path);
		if(c == null)
		{
			c = new Category();
			c.setName(name);
			c.setPath(path);
			c.setParent(getCategoryByUuid(parentId));
			c.setDepth(depth);
			c.setCategoryType(InitCategoryType.getCategoryTypeByUuid(InitCategoryType.createOrGetUuidByName(type)));
			
			try
			{
				transaction = session.beginTransaction();
				uuid = (Long)session.save(c);
				transaction.commit();
			}
			catch(HibernateException he)
			{
				he.printStackTrace();
			}
		}
		else
		{
			uuid = c.getUuid();
		}
		
		session.close();
		return uuid ;
	}
}
